package com.kaiwukj.android.ufamily.mvp.ui.page.active;

import android.os.Bundle;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.kaiwukj.android.mcas.di.component.AppComponent;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.app.base.review.BaseMvpActivity;
import com.kaiwukj.android.ufamily.app.constant.ARouterUrlKt;
import com.kaiwukj.android.ufamily.di.component.DaggerHomeV20Component;
import com.kaiwukj.android.ufamily.di.module.HomeV20Module;
import com.kaiwukj.android.ufamily.mvp.http.entity.event.HomeEvent;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.ActiveResult;
import com.kaiwukj.android.ufamily.mvp.ui.page.home.home.active.ActivePresenter;
import com.kaiwukj.android.ufamily.mvp.ui.page.home.home.contract.HomeV20Contract;
import com.kaiwukj.android.ufamily.mvp.ui.widget.SpaceItemDecoration;
import com.qmuiteam.qmui.widget.QMUIEmptyView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import org.greenrobot.eventbus.Subscribe;
import org.jetbrains.annotations.NotNull;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = ARouterUrlKt.MyActiveUrl)
public class MyActiveActivity extends BaseMvpActivity<ActivePresenter> implements HomeV20Contract.ActiveView {


    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.rv_list)
    RecyclerView mRvList;
    @BindView(R.id.empty_view)
    QMUIEmptyView emptyView;

    private MyActiveListAdapter mListAdapter;

    private int page = 1;
    private int pageSize = 12;

    private boolean isLoadingMore = false;


    @OnClick(R.id.container_left)
    void onBackClick() {
        onBackPressedSupport();
    }

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        super.setupActivityComponent(appComponent);
        DaggerHomeV20Component.builder()
                .appComponent(appComponent)
                .homeV20Module(new HomeV20Module(this))
                .build().inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_active_my;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tvTitle.setText("我的活动");
        refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                isLoadingMore = true;
                mPresenter.fetchActiveMy(++page, pageSize);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mPresenter.fetchActiveMy(page = 1, pageSize);
            }
        });
        mRvList.setLayoutManager(new LinearLayoutManager(this));
        mRvList.addItemDecoration(new SpaceItemDecoration(15));
        mListAdapter = new MyActiveListAdapter();
        mRvList.setAdapter(mListAdapter);

        mListAdapter.setOnItemClickListener((adapter, view, position) ->
                ARouter.getInstance().build(ARouterUrlKt.EventDetail)
                        .withInt("id", (int) adapter.getItemId(position))
                        .navigation());
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        emptyView.show(true);
        mPresenter.fetchActiveMy(page = 1, pageSize);
    }

    @SuppressWarnings("unchecked")
    @Override
    public void onFetchActive(@NotNull List<? extends ActiveResult> results) {
        emptyView.hide();
        if (isLoadingMore) {
            isLoadingMore = false;
            mListAdapter.addData(results);
            if (results.size() < pageSize) {
                refreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                refreshLayout.finishLoadMore(true);
            }
        } else {
            refreshLayout.finishRefresh(true);
            mListAdapter.setList((List<ActiveResult>) results);
        }
        if (mListAdapter.getItemCount() == 0) {
            emptyView.show("", "暂没有活动!");
        }
    }

    @Override
    public void onTaskErr(int taskId) {
        super.onTaskErr(taskId);
        refreshLayout.finishRefresh(false);
        refreshLayout.finishLoadMore(false);
        emptyView.show("", "数据加载失败!");
    }

    @Override
    public void onFetchActiveDetail(@NotNull ActiveResult result) {

    }

    @Subscribe
    public void onHomeEvent(HomeEvent homeEvent) {
        if (homeEvent.getEvent() == HomeEvent.UPDATE_DATA) {
            mPresenter.fetchActiveMy(page = 1, pageSize);
        }
    }
}
